package com.gskj.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gskj.commonutils.ResultJson;
import com.gskj.educenter.entity.MemberRegisterVO;
import com.gskj.educenter.entity.UcenterMember;
import com.gskj.eduservice.client.UCenterClient;
import com.gskj.eduservice.entity.EduComment;
import com.gskj.eduservice.service.IEduCommentService;
import com.gskj.servicebase.exceptionhandler.GuLiException;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author Connor
 * @since 2020-08-16
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/comment")
public class EduCommentController {

    @Autowired
    private IEduCommentService eduCommentService;

    // 注入远程调用接口
    @Autowired
    private UCenterClient uCenterClient;


    /***
     * 分页查询 评论信息
     * @param page  页面
     * @param pageSize  页条数
     * @param courseId  课程id
     *
     * @return
     */
    @GetMapping("/get/commont/front/pagelist/{page}/{pagesize}")
    public ResultJson getCommontPage(
            @PathVariable("page") Integer page,
            @PathVariable("pagesize") Integer pageSize,
            @RequestParam("courseId") String courseId
    ) {

        // 创建 分页 对象
        Page<EduComment> eduCommentPage = new Page<>(page, pageSize);


        //构造 查询条件
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();

        // 构建查询 条件, 根据 课程id 分页查询
        wrapper.eq("course_id", courseId);
        // 查询分页
        Map<String, Object> map = eduCommentService.getCommontPageList(eduCommentPage, wrapper);

        //返回数据
        return ResultJson.success().data(map);
    }

    /***
     *  添加课程评论信息
     * @param c
     * @return
     */
    @PostMapping("/add/commont/front")
    public ResultJson addCommontInfo(EduComment c, HttpServletRequest request) {

        /**----------------异常待处理----------------*/
        //TODO 调用 远程调用服务 ok
//        uCenterClient.registerMember(new MemberRegisterVO());

        // 远程调用接口 根据 会员id获取 会员的基本信息 ，error .
        //TODO 怀疑 参数为request时 导致的异常
        /***
         * 错误描述
         * Caused by: feign.codec.EncodeException: Could not write JSON: getInputStream() has already been called for this request; nested exception is com.fasterxml.jackson.databind.JsonMappingException: getInputStream() has already been called for this request (through reference chain: org.apache.catalina.connector.RequestFacade["reader"])
         * **/
//        uCenterClient.parseToken(request);
        /**-----------------异常待处理---------------*/


        // 获取请求头中携带的  token信息
        String token = request.getHeader("token");

        if (StringUtils.isEmpty(token)) {
            throw new GuLiException(20001, "请现在登陆后再进行评论！");
        }

        // 调用远程的方法 ,  重写 方法 参数为 token 字符串
        ResultJson json = uCenterClient.parseTokenWithStr(token);

        /**-------------------------使用远程调用服务,返回json中 object类型数据会被默认转成 key-value的 LinkedHashMap-------------------------------*/
//         根据id 获取 会员信息
//        UcenterMember member =
//                (UcenterMember) (json.getData().get("memberInfo"));

        /**-------------------------解决方式. 使用ObjectMapper对象,进行 对象数据浅赋值-------------------------------*/
        /**-------------------------解决方式. 使用ObjectMapper对象,进行 对象数据浅赋值,会报错  日期转化Json异常 ,在UcenterMember对象的日期类型上添加
         * 注解  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 指定json 转化格式-------------------------------*/

        ObjectMapper mapper = new ObjectMapper();
        UcenterMember member = mapper.convertValue(json.getData().get("memberInfo"), UcenterMember.class);
        /**-------------------------解决方式.-------------------------------*/

//        封装 插入信息
        c.setAvatar(member.getAvatar());
        c.setNickname(member.getNickname());
        c.setMemberId(member.getId());

//         调用 业务执行添加
        eduCommentService.addCommont(c);

        return ResultJson.success();
    }
}

